﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Manage
{
    public partial class Color : MgConfig
    {
        private int jTypeId;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Color.aspx");
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("JName = '颜色'");
            List<Jy123.Model.Public_JType> listJType = BLLJType.GetModelList(strWhere.ToString());
            bool hasJType = listJType.Count > 0;
            noBigTypeSpan.Visible = !hasJType;
            colorDiv.Visible = hasJType;
            if (hasJType)
            {
                jTypeId = listJType[0].ID;
            }            
            if (!Page.IsPostBack)
            {
                
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Color color = e.Item.DataItem as Jy123.Model.Public_Color;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = color.ID.ToString();
                Label nameLabel = e.Item.FindControl("colorNameLabel") as Label;
                Label valueLabel = e.Item.FindControl("colorValueLabel") as Label;
                Label imageLabel = e.Item.FindControl("colorImageLabel") as Label;
                imageLabel.Style.Remove("background-color");
                imageLabel.Style.Add("background-color", color.ColorValues);
                nameLabel.Text = color.CName;
                valueLabel.Text = color.ColorValues;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Color BLLColor = new Jy123.BLL.Public_Color();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Color color = BLLColor.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                colorNameTextBox.Text = color.CName;
                colorValueTextBox.Value = color.ColorValues;                
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLColor.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Color BLLColor = new Jy123.BLL.Public_Color();

            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Color color = new Jy123.Model.Public_Color();
                SetColor(color);
                BLLColor.Add(color);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Color color = BLLColor.GetModel(id);
                SetColor(color);
                BLLColor.Update(color);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            colorNameTextBox.Text = string.Empty;                        
        }

        private void SetColor(Jy123.Model.Public_Color color)
        {
            color.FJType = jTypeId;
            color.CName = colorNameTextBox.Text;
            color.ColorValues = colorValueTextBox.Value;            
        }

        private void LoadInitialValue()
        {
            colorValueTextBox.Value = "#ffffff";
            Jy123.BLL.Public_Color BLLColor = new Jy123.BLL.Public_Color();
            List<Jy123.Model.Public_Color> listcolor = BLLColor.GetModelList(string.Empty);
            colorRepeater.DataSource = listcolor;
            colorRepeater.DataBind();
            colorRepeater.Visible = listcolor.Count > 0;
        }
    }
}
